package com.atguigu.srb.core.controller.api;

import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.result.R;
import com.atguigu.srb.core.service.BorrowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/core/borrower")
@Api(tags = "借款人")
@Slf4j
public class WebBorrowerController {

    @Autowired
    BorrowerService borrowerService;

    @ApiOperation(value = "保存借款人信息")
    @PostMapping("/auth/save")
    public R saveBorrower(@RequestBody BorrowerVO borrowerVO, HttpServletRequest request) {
        // 验证是否登录,token验证
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);

        // 调用保存方法
        borrowerService.saveBorrowerVOByUserId(borrowerVO, userId);

        // 返回结果
        return R.ok().message("借款人信息保存成功");
    }

    @ApiOperation(value = "获取借款人状态")
    @PostMapping("/auth/getBorrowerStatus")
    public R getBorrowerStatus(HttpServletRequest request) {
        // 校验token检查用户是否登录
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        // 调用业务方法进行查询状态
        Integer status = borrowerService.getBorrowerStatus(userId);
        // 返回结果数据
        return R.ok().data("borrowerStatus", status);
    }



}
